<template>
  <div class="article-page">
    <!--
      header
      footer
      section
      aside
      nav
      ...
    -->
    <nav class="my-nav van-hairline--bottom">
      <a href="javascript:;" @click="fn2('weight_desc')" :class="{active:sorter==='weight_desc'}">推荐</a>

      <a href="javascript:;" @click="fn2(null)" :class="{active:sorter===null}">最新</a>

      <div class="logo"><img src="@/assets/logo.png" alt=""></div>
    </nav>
<van-list @load="getList" v-model="loading" :finished="finished" finished-text="没有更多了">
  <ArticleItem v-for="item in list" :key="item.id" :item="item"></ArticleItem>
</van-list>
</div>
</template>

<script>
// import request from '@/utils/request'
import { getArticleApi } from '@/api/article'
export default {
  name: 'ArticlePage',
  data () {
    return {
      list: [],
      current: 1,
      total: 0,
      pageSize: 10,
      sorter: 'weight_desc',
      pageTotal: 0,
      loading: false,
      finished: false
    }
  },
  created () {
    // this.getList()
  },
  methods: {
    fn () {
      console.log('点击了')
    },
    async getList () {
      const res = await getArticleApi(this.current, this.pageSize, this.sorter)
      console.log(res)
      // this.list = res.data.rows
      this.list.push(...res.data.rows)
      this.pageTotal = res.data.pageTotal
      this.total = res.data.total
      this.current++
      this.loading = false
      if (this.current > this.pageTotal) {
        this.finished = true
      }
    },
    fn2 (value) {
      this.sorter = value
      this.list = []
      this.current = 1 // current重置，重新加载第一页
      this.total = 0
      this.pageTotal = 0
      this.finished = false // finished重置，重新有数据可以加载了
      // this.loading = false
      // 手动加载更多   需要手动将loading改成true，表示正在加载中（避免重复触发）
      this.loading = true
      this.getList()
    }
  }
}
</script>

<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      > img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}

</style>
